package top.lyjwn.todo.weinxin.mch.controller;

import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import top.lyjwn.todo.aop.token.ExemptToken;
import top.lyjwn.todo.common.utils.WXNotify;
import top.lyjwn.todo.weinxin.data.mcg2022.jsapi.JsapiPayCallBack;
import top.lyjwn.todo.weinxin.mch.service.WXPayNotarize2022;

import jakarta.servlet.http.HttpServletRequest;

/**
 * 微信支付控制器
 * 
 * @author luoyujie
 *
 */
@RestController
@RequestMapping(PayController2022.CONTROLLER_NAME)
public class PayController2022 {


	@Autowired(required = false)
	private WXPayNotarize2022 wxNotarizeService;

	/**
	 * 支付控制器名称
	 */
	public final static String CONTROLLER_NAME="/pay";

	/**
	 * 支付回调，支付通知路径
	 */
	public final static String  PAY_NOTIFY_PATH = "/jsapiv3/pay";

	/**
	 * 退款回调，退款通知路径
	 */
	public final static String REFUND_NOTIFY_PATH = "/jsapiv3/refund";



	/**
	 * 支付通知，回调接口
	 *
	 * @param request
	 */
	@ExemptToken
	@RequestMapping(value = PAY_NOTIFY_PATH, method = RequestMethod.POST)
	public String notarizePay(HttpServletRequest request) {
		String resultStr = WXNotify.getString(request);
		JsapiPayCallBack jsapiPayCallBack = JSONObject.parseObject(resultStr, JsapiPayCallBack.class);
		String Wechatpay_Signature = request.getHeader("Wechatpay-Signature");
		if (Wechatpay_Signature==null || jsapiPayCallBack==null)
		{
			//如果签名信息为空那么直接让他去死
			return "go die!";
		}

		return "";
	}


	/**
	 * 退款通知，回调接口
	 *
	 * @param request
	 */
	@ExemptToken
	@RequestMapping(value = REFUND_NOTIFY_PATH, method = RequestMethod.POST)
	public String notarizeRefund(HttpServletRequest request) {
		String resultStr = WXNotify.getString(request);
		JsapiPayCallBack jsapiPayCallBack = JSONObject.parseObject(resultStr, JsapiPayCallBack.class);
		String Wechatpay_Signature = request.getHeader("Wechatpay-Signature");
		if (Wechatpay_Signature==null || jsapiPayCallBack==null)
		{
			//如果签名信息为空那么直接让他去死
			return "go die!";
		}
		return "";
	}






}
